{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap=cv2.VideoCapture(0)\n",
    "face_cascade = cv2.CascadeClassifier('D:\\\\ProgramData\\\\Anaconda3\\\\Lib\\\\site-packages\\\\cv2\\\\data\\\\haarcascade_frontalface_default.xml')\n",
    "eye_cascade = cv2.CascadeClassifier('D:\\\\ProgramData\\\\Anaconda3\\\\Lib\\\\site-packages\\\\cv2\\\\data\\\\haarcascade_eye_tree_eyeglasses.xml')\n",
    "font = cv2.FONT_HERSHEY_SIMPLEX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "facePos = np.array([[0,0,0,0],[0,0,0,0]])\n",
    "eyePos = np.array([0,0,0,0])\n",
    "staticPos = np.array([0,0,0,0])\n",
    "staticEyePos = np.array([0,0,0,0])\n",
    "isStatic = False\n",
    "settled = False\n",
    "def sharpen(image):\n",
    "    kernel = np.array([[0,-1,0],[-1,4.5,-1],[0,-1,0]],np.float32)#Laplace算子实现滤波器\n",
    "    dst = cv2.filter2D(image,-1,kernel=kernel)\n",
    "    return dst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "while(cap.isOpened()):\n",
    "    ret,frame = cap.read()\n",
    "    #frame = cv2.imread('D:\\\\1.jpg')\n",
    "    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
    "    gray = cv2.equalizeHist(gray)\n",
    "    faces = face_cascade.detectMultiScale(gray, 1.3, 5)\n",
    "    \n",
    "    shp = sharpen(frame)\n",
    "    shp = cv2.GaussianBlur(shp,(7,7),0)\n",
    "    gshp = cv2.cvtColor(shp, cv2.COLOR_BGR2GRAY)\n",
    "    canny = cv2.Canny(gshp,55,100)\n",
    "    \n",
    "    for (x,y,w,h) in faces:\n",
    "        frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)\n",
    "        roi_gray = gray[y:y+h, x:x+w]\n",
    "        roi_color = frame[y:y+h, x:x+w]\n",
    "        facePos = faces\n",
    "        eyes = eye_cascade.detectMultiScale(roi_gray)\n",
    "        try:\n",
    "            if(len(eyes)>=1 or eyes!=None):\n",
    "                eyePos = list(eyes)\n",
    "        except:\n",
    "            pass\n",
    "        for (ex,ey,ew,eh) in eyes:\n",
    "            cv2.circle(roi_color,(int(ex+ew/2),int(ey+eh/2)),2,(0,255,0),-1)\n",
    "            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\n",
    "            pass\n",
    "    \n",
    "    i = 0\n",
    "    try:\n",
    "        if(isStatic==False):\n",
    "            settled = False\n",
    "        for (eex,eey,eew,eeh) in eyePos:\n",
    "            cv2.putText(frame,f'posX={eex},posY={eey},w={eew},h={eeh}',(10,300-i*30),font,0.8,(255,255,255),2)\n",
    "            i=i+1\n",
    "    except:\n",
    "        pass\n",
    "    i=1\n",
    "    if(isStatic):\n",
    "        if(settled == False):\n",
    "            settled = True\n",
    "            staticEyePos = eyePos\n",
    "            staticPos = facePos\n",
    "        cv2.putText(frame,'Static',(10,330),font,0.8,(255,255,255),2)\n",
    "        for (eex,eey,eew,eeh) in staticEyePos:\n",
    "            cv2.putText(frame,f'Static Eye Pos(Rlt to Face),posX={eex},posY={eey},w={eew},h={eeh}',(10,330+i*30),font,0.8,(255,255,255),2)\n",
    "            i=i+1\n",
    "        cv2.putText(frame,f'Static Face Position,posX={staticPos[0][0]},posY={staticPos[0][1]}',(10,330+i*30),font,0.8,(255,255,255),2)\n",
    "    ret,thresh = cv2.threshold(canny,127,255,0)\n",
    "    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n",
    "    ii=0\n",
    "    \n",
    "    for index,contr in enumerate(contours):\n",
    "        M=cv2.moments(contr)\n",
    "        if(M['m00'] != 0):\n",
    "            cx=int(M['m10']/M['m00'])\n",
    "            cy=int(M['m01']/M['m00'])\n",
    "            x,y,w,h = cv2.boundingRect(contr)\n",
    "        else:\n",
    "            continue\n",
    "        for po in eyePos:\n",
    "            frame = cv2.rectangle(frame,(po[0]+facePos[0][0],po[1]+facePos[0][1]),(po[0]+po[2]+facePos[0][0],po[1]+po[3]+facePos[0][1]),(0,0,255),2)\n",
    "            if(x>po[0]+facePos[0][0] and y>po[1]+facePos[0][1] and x+w<po[0]+po[2]+facePos[0][0] and y+h<po[1]+po[3]+facePos[0][1]):\n",
    "                frame = cv2.drawContours(frame,contours,index,(0,0,255),3)\n",
    "    #cv2.drawContours(frame,contours,-1,(0,0,255),3)\n",
    "    \n",
    "    cv2.imshow('frame',frame)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('s'):\n",
    "        isStatic = bool(1-isStatic)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
